package com.axino.accessPlatform.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import com.axino.accessPlatform.bean.PrivateData;
import com.axino.accessPlatform.bean.PublicData;
import com.axino.accessPlatform.bean.RequestMode;
import com.axino.accessPlatform.exception.OpensnsException;
import com.axino.accessPlatform.service.OpenApiV1;
import com.axino.accessPlatform.util.EInvoiceSign;
import com.axino.accessPlatform.util.SecurityUtil;
import com.axino.accessPlatform.util.ValidataUtil;

public class Eget {

/**
 * 电子发票  开票请求
 * @param args
 */
public static void main(String[] args) {
		
		//定义请求头数据
		Map<String,String> headers = getHeaders();
		//定义请求体中的公共数据
		PublicData pdData = getPublicData();
		//定义请求体中的业务数据
		PrivateData<Object> pvData = new PrivateData<Object>();
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		List<Map<String,Object>> list_invoiceDetail = new ArrayList<Map<String,Object>>();
		Map<String,Object> map_s = new HashMap<String,Object>();
		Map<String,Object> map_outsign = new HashMap<String,Object>();
		Map<String,Object> map_order = new HashMap<String,Object>();
		Map<String,Object> map_invoiceDetails = new HashMap<String,Object>();
		long startTime = System.currentTimeMillis();
        map_s.put("taxNum", SecurityUtil.taxNum_value);
        map_outsign.put("timestamp", ""+startTime);
        map_outsign.put("appid", SecurityUtil.appid_value);
        map_outsign.put("businessSign", EInvoiceSign.eInvoiceEncodeData(""+startTime));
        map_s.put("outSign",map_outsign);
        map_order.put("pushMode", "1");
        map_order.put("orderNo", "6699887755");
        map_order.put("salerTaxNum", "339901999999142");
        map_order.put("salerAddress", "杭州市中河中路222号平海国际商务大厦5楼");
        map_order.put("salerTel", "0571-87022168");
        map_order.put("salerAccount", "东亚银行杭州分行131001088303400");
        map_order.put("clerk", "袁牧庆");
        map_order.put("payee", "络克");
        map_order.put("checker", "朱燕");
        map_order.put("invoiceDate", "2016-06-15 01:51:41");
        map_order.put("taxIncludedAmount", "46.79");
        map_order.put("invoiceType", "1");
        map_order.put("tax", "6.8");
        map_order.put("taxExcludedAmount", "39.99");
        map_order.put("buyerAddress", null);
        map_order.put("buyerPhone", "13185029520");
        map_order.put("buyerTaxNum", "110101TRDX8RQU1");
        map_order.put("buyerName", "个人");
        map_order.put("buyerAccount", null);
        map_order.put("invoiceCode", "11");
        map_order.put("invoiceNum", "11");
        map_order.put("remark", "开票机号为02 请前往诺诺网(www.jss.com.cn)查询发票详情");
        map_order.put("listFlag", "0");
        map_order.put("listName", "1111");
        map_order.put("proxyInvoiceFlag", "0");
        map_invoiceDetails.put("goodsName", "1");
        map_invoiceDetails.put("specType", "1");
        map_invoiceDetails.put("unit", "1");
        map_invoiceDetails.put("withTaxFlag", "1");
        map_invoiceDetails.put("num", "1");
        map_invoiceDetails.put("price", "19.99");
        map_invoiceDetails.put("goodsCode", "1090511030000000000");
        map_invoiceDetails.put("invoiceLineProperty", "0");
        map_invoiceDetails.put("favouredPolicyFlag", "0");
        map_invoiceDetails.put("favouredPolicyName", null);
        map_invoiceDetails.put("zeroRateFlag", null);
        map_invoiceDetails.put("taxRate", "0.17");
        list_invoiceDetail.add(map_invoiceDetails);
        map_order.put("invoiceDetail", list_invoiceDetail);
        map_s.put("order", map_order);
		list.add(map_s);
		System.out.println(JSONArray.fromObject(list).toString());
		pvData.setServicedata(list);
		RequestMode requestMode = getRequestMode(pdData,pvData);
		OpenApiV1 sdk =  new OpenApiV1();
		String result = "";
		try {
			//调用服务
			result = sdk.handle(SecurityUtil.url,headers, requestMode);
			if(!ValidataUtil.isEmpty(result)){
				System.out.println("服务端的响应："+result);
			}
		} catch (OpensnsException e) {
			  System.out.printf("Request failed 【"+e.getErrorCode()+":"+e.getMessage()+"】");
			  e.printStackTrace();
		} 
	  }
	
	//定义请求头数据
	private static Map<String,String> getHeaders (){
		Map<String,String> headers = new HashMap<String, String>();
		headers.put("appKey", ""); //用户申请的appkey  J2VXxB2h_sbox   CmtXqndv
		headers.put("accessToken", ""); //用户Oauth登录后得到的令牌access token
		headers.put("compress", "");//压缩方式：提供GZIP 置空“”不压缩
		headers.put("signMethod", "AES"); //加密方式：提供AES ，不可为空
		headers.put("dataType", "json"); //数据请求格式： JSON/XML
		headers.put("appRate", SecurityUtil.app_rate); //app并发请求数  ，平台默认10如需升级请联系开放平台
		return headers;
	}
	
	//定义请求体中的公共数据
	private static PublicData getPublicData(){
		PublicData pdData = new PublicData();
		pdData.setVersion("1.0"); //API版本
		pdData.setTimestamp(String.valueOf(System.currentTimeMillis()));
		pdData.setMethod("nuonuo.electronInvoice.requestBilling");//API名称
		return pdData;
	}
	
	private static RequestMode getRequestMode(PublicData pdata,PrivateData<Object> pvData){
		RequestMode requestMode = new RequestMode();
		requestMode.setPrivate(pvData);
		requestMode.setPublic(pdata);
		return requestMode;
	}

}
